package com.bridge.common.base;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import com.bridge.common.R;
import com.bridge.common.bean.Event;
import com.bridge.common.dialog.VDialog;

import com.bridge.common.interfaces.IHttpCallback;
import com.bridge.common.utils.ActivityUtil;
import com.bridge.common.utils.EventBusUtil;
import com.bridge.common.utils.StatusBarUtil;


import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    public static BaseActivity mContext = null;

    @Override
    public void onCreate(Bundle savedInstanceState){
        // 禁止截屏
//        SecureUtils.setFobiddenScreenShot(this);
        ActivityUtil.addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        beforSetContentView();
        setContentView(layoutId());

        initView();
        setListenner();

        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
        mContext = this ;
    }
    /**
     * 获取当前处于前台的activity
     */
    public static BaseActivity getCurrentActivity() {
        return mContext;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 一般方法,可供在设置setContentView前做些操作
     */
    public void beforSetContentView() {
        StatusBarUtil.setStatusBarMode(this,true, R.color.background);
    }


    @Override
    public void onClick(View view) {

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }

    @Override
    protected void onDestroy() {
        ActivityUtil.finishActivity(this);
        super.onDestroy();
    }

    /**
     * 获取bind对象
     * @param <T>
     * @return
     */
    public <T extends ViewDataBinding> T getBind() {
        return DataBindingUtil.setContentView(this, layoutId());
    }




    /**
     * 进度条加载回调，可统一加载
     * @param text
     */
    public void onLoading(String text) {
        VDialog.getInstance().showLoadingDialog(this, text, true, true);
    }



    public void onCancelProgressDialog() {
        VDialog.getInstance().hideLoadingDialog();
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(Event event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe( sticky = true)
    public void onStickyEventBusCome(Event event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(Event event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(Event event) {

    }
    /***********************************EventBus  end***********************************/

    protected abstract int layoutId();
    protected abstract void initView();
    protected abstract void setListenner();
}
